import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemUserApi } from '#/api';

import { getDeptList } from '#/api/system/dept';
import { getAssignableRoleList } from '#/api/system/role';
import { $t } from '#/locales';

export function useFormSchema(id?: string): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.user.username'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'realName',
      label: $t('system.user.realName'),
      rules: 'required',
    },
    {
      component: 'Select',
      componentProps: {
        options: [
          { label: '男', value: 'MALE' },
          { label: '女', value: 'FEMALE' },
          { label: '未知', value: 'UNKNOWN' },
        ],
        class: 'w-full',
      },
      defaultValue: 'UNKNOWN',
      fieldName: 'gender',
      label: $t('system.user.gender'),
    },
    {
      component: 'VbenInputPassword',
      componentProps: {
        defaultValue: '123456',
      },
      fieldName: 'password',
      label: $t('system.user.password'),
      rules: 'required',
      dependencies: {
        triggerFields: ['id'],
        show: () => !id,
      },
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: $t('system.user.email'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: $t('system.user.phone'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'address',
      label: $t('system.user.address'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: true,
      fieldName: 'status',
      label: $t('system.user.status'),
      rules: 'required',
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: getDeptList,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
      },
      fieldName: 'department.id',
      label: $t('system.dept.name'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        mode: 'multiple',
        allowClear: true,
        api: getAssignableRoleList,
        labelField: 'name',
        valueField: 'id',
        class: 'w-full',
      },
      fieldName: 'roleIds',
      label: $t('system.user.roles'),
    },
    {
      component: 'Textarea',
      fieldName: 'remark',
      label: $t('system.user.remark'),
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: '用户名',
    },
    { component: 'Input', fieldName: 'email', label: $t('system.user.email') },
    { component: 'Input', fieldName: 'phone', label: $t('system.user.phone') },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
        ],
      },
      fieldName: 'status',
      label: $t('system.user.status'),
    },
  ];
}

export function useColumns<T = SystemUserApi.SystemUser>(
  onActionClick: OnActionClickFn<T>,
  onStatusChange?: (newStatus: any, row: T) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'username',
      title: $t('system.user.username'),
      width: 200,
    },
    {
      field: 'realName',
      title: $t('system.user.realName'),
      width: 200,
    },
    {
      field: 'department.name',
      title: $t('system.dept.name'),
      width: 200,
    },
    {
      field: 'phone',
      title: $t('system.user.phone'),
      width: 200,
    },
    {
      field: 'email',
      title: $t('system.user.email'),
      width: 200,
    },
    {
      field: 'address',
      title: $t('system.user.address'),
      width: 200,
    },
    {
      cellRender: {
        attrs: {
          beforeChange: onStatusChange,
        },
        name: 'CellSwitch',
      },
      field: 'status',
      title: $t('system.user.status'),
      width: 100,
    },
    {
      field: 'remark',
      minWidth: 100,
      title: $t('system.user.remark'),
    },
    {
      field: 'createTime',
      title: $t('system.user.createTime'),
      width: 200,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'username',
          nameTitle: $t('system.user.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
      },
      field: 'operation',
      fixed: 'right',
      title: $t('system.user.operation'),
      width: 130,
    },
  ];
}
